Skip to content

fix(image): use CSS styles for width/height to support percentage values#9966

Merged
mscolnick merged 1 commit into
marimo-team:mainfrom
allin2:fix/image-video-height-100
Jul 1, 2026
Merged

fix(image): use CSS styles for width/height to support percentage values#9966
mscolnick merged 1 commit into
marimo-team:mainfrom
allin2:fix/image-video-height-100

Conversation

@allin2

@allin2 allin2 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The ImageOutput component used HTML attributes for width/height, which only accept pixel values. This prevented percentage values like '100%' from working.

Motivation

Fixes #9810

Users reported that setting height=100% on images and videos did not work.

Changes

  • fix: Changed ImageOutput to use CSS styles instead of HTML attributes for width/height
  • CSS styles support both pixel values and percentage values
  • Numeric values are still converted to pixel strings (e.g., 100 → "100px")
  • String values like "100%" are passed through as-is

Validation

  • mo.image(src, height=100) still works (converted to "100px")
  • mo.image(src, height="100%") now works correctly
  • mo.image(src, width="50%") now works correctly

Checklist

  • Code follows the project's style guidelines
  • Tests pass
  • No unrelated changes included
  • Commit message follows Conventional Commits format

Review in cubic

The ImageOutput component used HTML attributes for width/height, which
only accept pixel values. This prevented percentage values like '100%'
from working.

Changed to use CSS styles instead of HTML attributes, which support
both pixel values and percentage values.

Fixes marimo-team#9810
@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Jun 23, 2026 12:25pm

Request Review

@mscolnick mscolnick self-requested a review June 29, 2026 17:36
@mscolnick mscolnick added the bug Something isn't working label Jun 30, 2026
@mscolnick mscolnick enabled auto-merge (squash) June 30, 2026 13:51
@kirangadhave kirangadhave removed their request for review June 30, 2026 15:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the frontend image output renderer so width/height can be expressed using CSS (supporting percentage values like "100%") instead of relying on <img> HTML attributes that effectively only work with pixel/integer sizing.

Changes:

  • Broadened ImageOutput props to accept number | string for width/height.
  • Converted numeric dimensions to "${n}px" and passed string dimensions through unchanged.
  • Switched from <img width/height> attributes to style={{ width, height }}.

Comment on lines +20 to +21
// Convert numeric values to pixel strings, pass string values (like "100%") as-is
const style: React.CSSProperties = {};
Comment on lines +20 to +27
// Convert numeric values to pixel strings, pass string values (like "100%") as-is
const style: React.CSSProperties = {};
if (width !== undefined) {
style.width = typeof width === "number" ? `${width}px` : width;
}
if (height !== undefined) {
style.height = typeof height === "number" ? `${height}px` : height;
}
Comment on lines 29 to 33
return (
<span className={className}>
<img src={src} alt={alt} width={width} height={height} />
<img src={src} alt={alt} style={style} />
</span>
);
@mscolnick mscolnick disabled auto-merge July 1, 2026 13:38
@mscolnick mscolnick merged commit ad162eb into marimo-team:main Jul 1, 2026
28 of 30 checks passed
akshayka added a commit that referenced this pull request Jul 10, 2026
This PR changes images to carry their width and heights as HTML
attributes instead of as inline styles. This in turn allows our
stylesheet to correctly scale images when rendered in (for example)
hstacks.

String values like "100%" are only valid in CSS and still go through the
style attribute, preserving the percentage support added in #9966.

In particular, this prevents matplotlib figures (which carry pixel
dimensions in their mimebundle metadata since #9144) from being squished
or distorted.

Before change:

<img width="802" height="548" alt="image"
src="https://github.com/user-attachments/assets/748c5568-6689-4896-988a-7eebe5022af2"
/>

With fix:

<img width="722" height="382" alt="image"
src="https://github.com/user-attachments/assets/e0e41052-7a71-4891-b869-ccc27341e6c9"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

height=100% does not work on images and videos

3 participants